import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('d:\\dog.jpg')

# kernel = np.ones((3, 3), np.uint8)

# kernel =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# kernel =cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
kernel=cv2.getStructuringElement(cv2.MORPH_CROSS,(7,7))

erosion = cv2.erode(img, kernel, iterations=1)
dilation = cv2.dilate(img,kernel,iterations = 1)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', 800, 800)  # 定义frame的大小
cv2.imshow('image', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()